import React from 'react';
import { Routes, Route, Navigate } from 'react-router-dom';
import Login from './login/index';
import AdminLayout from './components/AdminLayout';
import Dashboard from './pages/Dashboard';
import UserManagement from './pages/UserManagement';
import CategoryManagement from './pages/CategoryManagement';
import OrderManagement from './pages/order/OrderManagement';
import PermissionGuard from './components/PermissionGuard';
import { PermissionCode } from './types/auth';

// 路由守卫组件
const PrivateRoute: React.FC<{ children: React.ReactNode }> = ({ children }) => {
  const isAuthenticated = localStorage.getItem('adminToken') !== null;
  
  if (!isAuthenticated) {
    return <Navigate to="/admin/login" replace />;
  }

  return <>{children}</>;
};

// 仪表盘页面组件
const DashboardComponent: React.FC = () => {
  return (
    <div>
      <h1>仪表盘</h1>
      <p>欢迎使用后台管理系统</p>
    </div>
  );
};

// 用户管理页面组件
const Users: React.FC = () => {
  return (
    <div>
      <h1>用户管理</h1>
      <p>用户管理页面（待实现）</p>
    </div>
  );
};

// 商品管理页面组件
const Products: React.FC = () => {
  return (
    <div>
      <h1>商品管理</h1>
      <p>商品管理页面（待实现）</p>
    </div>
  );
};

// 订单管理页面组件
const Orders: React.FC = () => {
  return (
    <div>
      <h1>订单管理</h1>
      <p>订单管理页面（待实现）</p>
    </div>
  );
};

const AdminRoutes: React.FC = () => {
  return (
    <Routes>
      {/* 登录页面路由 */}
      <Route path="/admin/login" element={<Login />} />
      
      {/* 需要认证的后台管理路由 */}
      <Route
        path="/admin"
        element={
          <PrivateRoute>
            <AdminLayout />
          </PrivateRoute>
        }
      >
        {/* 嵌套路由 */}
        <Route index element={<Navigate to="dashboard" replace />} />
        <Route path="dashboard" element={<Dashboard />} />
        
        {/* 用户管理 - 需要系统管理权限 */}
        <Route 
          path="users" 
          element={
            <PermissionGuard permission={PermissionCode.USER_MANAGE}>
              <UserManagement />
            </PermissionGuard>
          } 
        />
        
        {/* 商品管理 - 需要商品管理权限 */}
        <Route 
          path="products" 
          element={
            <PermissionGuard permission={PermissionCode.PRODUCT_MANAGE}>
              <div>
                <h1>商品管理</h1>
                <p>商品管理页面（待实现）</p>
              </div>
            </PermissionGuard>
          } 
        />
        
        {/* 订单管理 - 需要订单管理权限 */}
        <Route 
          path="orders" 
          element={
            <PermissionGuard permission={PermissionCode.ORDER_MANAGE}>
              <OrderManagement />
            </PermissionGuard>
          } 
        />
        
        {/* 分类管理 - 需要分类管理权限 */}
        <Route 
          path="categories" 
          element={
            <PermissionGuard permission={PermissionCode.CATEGORY_MANAGE}>
              <CategoryManagement />
            </PermissionGuard>
          } 
        />
      </Route>
      
      {/* 捕获所有其他后台路由，重定向到仪表盘 */}
      <Route path="/admin/*" element={<Navigate to="/admin/dashboard" replace />} />
    </Routes>
  );
};

export default AdminRoutes; 